package com.study.d012Components.listview;

import java.util.Observable;

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ListView.EditEvent;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
*@description 列表
*@author xtayhame
*date 2020年3月8日
*version
*since v1.0
*/
public class ListViewMain1 extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		ObservableList<String> datas=FXCollections.observableArrayList();
		datas.addAll("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w");
		ListView<String>  listView= new ListView<String>(datas);
		listView.setPrefSize(200, 150);




		//设置行高
		listView.setFixedCellSize(20);

		//阻止获得焦点
		//listView.setFocusTraversable(false);

		//设置第0个元素默认被选中    方式一
		listView.getFocusModel().focus(0);
		//设置第4个元素被选中	        方式二   （两种方式可以共存）
		listView.getSelectionModel().select(3);

		Button b1  = new Button("点击");

		anchorPane.getChildren().addAll(b1,listView);
		AnchorPane.setLeftAnchor(listView, 50.0);
		AnchorPane.setTopAnchor(listView, 50.0);

		stage.setScene(new Scene(anchorPane, 300, 300));
		stage.show();

		//默认单选， 这里设置多选
		listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

		//设置被选中
		listView.getSelectionModel().select("b");

		//设置可编辑
		listView.setEditable(true);
		//listView.setCellFactory(TextFieldListCell.forListView());

		//第一种方式： 对输入的字符串进行转换
		Callback<ListView<String>,ListCell<String>> callback= TextFieldListCell.forListView(new StringConverter<String>() {
			@Override
			public String toString(String object) {
				//初始化的时候 默认加上对应的字符串
				return object + " - world";
			}

			@Override
			public String fromString(String string) {
				//输入后回车 会 加上对应的字符串， 然后调用toString()方法
				return string + " - hello";
			}
		});



		listView.setCellFactory(callback);




		//开始编辑事件
		listView.setOnEditStart(new EventHandler<ListView.EditEvent<String>>() {
			@Override
			public void handle(EditEvent<String> event) {
				System.out.println("开始编辑");
				System.out.println("	index="+event.getIndex());
				System.out.println("    newValue="+ event.getNewValue());
			}
		});

		listView.setOnEditCancel(new EventHandler<ListView.EditEvent<String>>() {
			@Override
			public void handle(EditEvent<String> event) {
				System.out.println("取消编辑");
				System.out.println("	index="+event.getIndex());
				System.out.println("    newValue="+ event.getNewValue());
			}
		});

		listView.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() {

			@Override
			public void handle(EditEvent<String> event) {
				System.out.println("提交编辑");
				System.out.println("	index="+event.getIndex());
				System.out.println("    newValue="+ event.getNewValue());

				//JDK的bug, 这里需要重新设置一下
				datas.set(event.getIndex(), event.getNewValue());
			}
		});



		b1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				/*if(listView.getSelectionModel().getSelectionMode() ==SelectionMode.SINGLE){
					String item=listView.getSelectionModel().getSelectedItem();
					System.out.println("单选：" );
					System.out.println("	 "+item );
				}else if(listView.getSelectionModel().getSelectionMode() ==SelectionMode.MULTIPLE){

					ObservableList<String> obs=listView.getSelectionModel().getSelectedItems();
					System.out.println("多选：");
					for(String s:obs){
						System.out.println("	 "+s);
					}
				}*/

			}
		});

	}


}
